var objLogin = null;
var url = (document.location.href).toLowerCase();
function checkSessionResult(objUser) {
    var strUrlLocation = window.location.href;
    var strUrlLink = strPathLink_All;
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }
    //$("btnSendMail").onclick = sendForgotPass;
    var strHtml = '';
    if (objUser != "0") {
        if (typeof (objUser) == "object") {
            objLogin = objUser;
            strHtml = "<a href='" + strUrlLink + "profile/photos.aspx' class='aImgLogin'><img src='" + objUser.avatar + "' width='58px' height='58px'></a>" +
    	       "<p class='styleText7'>" + langSI()[0][13] + "</p>" +
    	       "<p class='styleText15 pNameMem'><a class='pNameMem' href='" + strUrlLink + "profile/photos.aspx'>" + objUser.nick + "</a></p>" +
            "<p class='styleText7'><a class='colorPF' href='" + strUrlLink + "profile/photos.aspx'>" + langSI()[0][15] + "</a></p>";
            $("divSignIn").innerHTML = strHtml;
            $("divRemem").innerHTML = '<p style="position:absolute;margin:-1px 0 0 -34px;width:225px"><span style="color:#00CCFE;font-size:13px;float:right;padding-left:3px">VND</span><strong class="moneyTK"> ' + objUser.money + '</strong><span style="font-size:13px;color:#ffffff;float:right">'+langSI()[0][15]+':</span></p>';
            $("btnSignIn").innerHTML = langSI()[0][12];
            $("btnSignIn").href = strUrlLink + "logout.aspx";
            $("btnSignIn").onclick = Logout;
            $("div_BodyHDSD").innerHTML = "";
            if ($("divAvaComm")) $("divAvaComm").down("img").src = objUser.avatar;
        }
    } else {
        strHtml = '<ul class="inner_SinginNew">' +
        	                    '<li>' +
            	                    '<p class="styleText7 pPopSI">' + langSI()[0][3] + ':</p>' +
                                   '<div class="div_txtNew">' +
					                    '<div class="txt_tlNew"></div>' +
					                    '<div class="txt_ctNew"></div>' +
					                    '<div class="txt_trNew"></div>' +
						                    '<div class="txt_clNew">	' +
							                    '<div class="txt_crNew">' +
  							                    '<div class="txt_inner_New">' +
     							                    '<input type="text" autocomplete="off" id="txtEmail" type="text" value=""  name=""/>' +
   							                    '</div>' +
						                    '</div>' +
				                    '</div>' +
			                    '<div class="txt_blNew"></div>' +
			                    '<div class="txt_cbNew"></div>' +
			                    '<div class="txt_brNew"></div>' +
		                    '</div>' +
                                '</li>' +
                                '<li class="li_PassSinginNew">' +
            	                    '<p class="styleText7 pPopSI">' + langSI()[0][4] + ':</p>' +
                                   '<div class="div_txtNew">' +
					                    '<div class="txt_tlNew"></div>' +
					                    '<div class="txt_ctNew"></div>' +
					                    '<div class="txt_trNew"></div>' +
						                    '<div class="txt_clNew">	' +
							                    '<div class="txt_crNew">' +
  							                    '<div class="txt_inner_New">' +
     							                    '<input type="password" id="txtPass" type="text" value=""  name=""/>' +
   							                    '</div>' +
						                    '</div>' +
				                    '</div>' +
			                    '<div class="txt_blNew"></div>' +
			                    '<div class="txt_cbNew"></div>' +
			                    '<div class="txt_brNew">' +
                                '</div>' +
		                    '</div>' +
                        '</li>' +
                    '</ul>';
        $("div_BodyHDSD").innerHTML = '<a class="styleText7 color18 aRegistry" id="a1"  href="' + strUrlLink + 'SignUp.aspx">' + langSI()[0][9] + '</a><p class="TwoLine"></p>';
        $("divRemem").innerHTML = '<div class="cbReTopSignin" id="div_cbTopSignin"><i class="i_CbReTopSignIE"></i></div><span  class="color9 styleText7">' + langSI()[0][10] + '</span><span class="styleText8 spnLineQuenMK">|</span><a href="javascript:void(0)" id="btnLostPass" class="color9 styleText7 aDKTopMC">' + langSI()[0][11] + '</a>';
        $("divSignIn").innerHTML = strHtml;
        $("btnSignIn").onclick = checkLogin;

        if (GetCookie("email")) {
            $("txtEmail").value = GetCookie("email");
            $('div_cbTopSignin').addClassName("cbCheckedTopSignin");
        }
        $('div_cbTopSignin').observe('click', function() {
            $(this).hasClassName("cbCheckedTopSignin") ? $("div_cbTopSignin").removeClassName("cbCheckedTopSignin") : $(this).addClassName("cbCheckedTopSignin");
        });
        $("btnLostPass").onclick = showLostPass;
        if ($("txtEmail")) {
            $("txtEmail").onkeypress = PressKey;
            $("txtPass").onkeypress = PressKey;
        }
    }
}
function PressKey(e) {
    return false;
    var keycode;
    var eID = utils.getElementFireEvent(e).id;
    e = e || window.event;
    if (e.keyCode == "13" || e.which == "13" || e.charCode == "13") {
        //alert(this.id);
        switch (this.id) {
            case "txtEmailTopBanner":
                insertEmailGiaTot();
                break;
            case "txtEmail": case "txtPass":
                checkLogin();
                break;
            case "txtSUEmail": case "txtSUNick": case "txtSUPass": case "txtSURePass": case "txtSUName": case "str_ddlDate": case "str_ddlMonth": case "str_ddlYear":
                SignUp();
                break;
        }
    }
}
///check login
function checkLogin() {
    var strUrlLocation = window.location.href;
    var strUrlLink = strPathLink_All;
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }
    if (checkMail($("txtEmail").value) == true) {
        if ($("div_cbTopSignin").className.indexOf("cbCheckedTopSignin") != -1)
            SetCookie("email", $("txtEmail").value, 60);
        else
            DeleteCookie("email", "");
        utils.call(strUrlLink + "login.aspx", { "flag": "check", "email": $("txtEmail").value, "pass": $("txtPass").value }, "POST", "checkLoginResult", "checkLoginResult");
    }
}
function checkLoginSI() {
    var strUrlLocation = window.location.href;
    var strUrlLink = strPathLink_All;
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }
    if (checkMail($("txtSIEmail").value) == true) {
        $("PMessSignin").innerHTML = "";
        if ($("div_cbSignin").className.indexOf("checkedDK") != -1)
            SetCookie("email", $("txtSIEmail").value, 60);
        else
            DeleteCookie("email", "");
        var strUrlLogin = window.location.href;
       /* if (strUrlLogin.toLowerCase().indexOf("logindemo.aspx") != -1) {
            utils.call((url.indexOf('/profile/') != -1 ? "../" : "") + "logindemo.aspx", { "flag": "check", "email": $("txtSIEmail").value, "pass": $("txtSIPass").value }, "POST", "checkLoginResultSI", "checkLoginResultSI");
        } else {*/
        utils.call(strUrlLink + "login.aspx", { "flag": "check", "email": $("txtSIEmail").value, "pass": $("txtSIPass").value }, "POST", "checkLoginResultSI", "checkLoginResultSI");
        //}
    } else {
        $("PMessSignin").innerHTML = langMess()[0][4];
    }
}
// logout
function Logout() {
    var strUrlLocation = window.location.href;
    var strUrlLink = strPathLink_All;
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }
    utils.call(strUrlLink+ "login.aspx", { "flag": "logout" }, "POST", "LogoutResult", "LogoutResult");
}
function LogoutResult() {
    var strUrlLogin = window.location.href;
    if (strUrlLogin.indexOf("#") != -1) {
        strUrlLogin = strUrlLogin.substring(0, strUrlLogin.indexOf("#"));
    }
    window.location.href = strUrlLogin;
}
function checkSession() {
    var strUrlLocation = window.location.href;
    var strUrlLink = strPathLink_All;
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }
    utils.call(strUrlLink + "login.aspx", { "flag": "checksession" }, "POST", "checkSessionResult", "checkSessionResult");
}
function checkLoginResultSI(strResult) {
    if (strResult != "0") {
        var strUrlLogin = window.location.href;
        if (strUrlLogin.indexOf("#") != -1) {
            strUrlLogin = strUrlLogin.substring(0, strUrlLogin.indexOf("#"));
        }
        //edit by name 26/3/2011
        if ($("txtCommentDeal")) {
            if ($("txtCommentDeal").value != "")
                SetCookie("Mobile_Comment", $("txtCommentDeal").value, 60);
            if (Global_IDComment != 0) {
                var value = Global_IDComment + "#@#" + $("Textarea2_" + Global_IDComment).value;
                SetCookie("Mobile_SubComment", value, 60);
            }
        }
        //--------------
        window.location.href = strUrlLogin;
    } else {
        $("PMessSignin").innerHTML = langSI()[0][0];
    }
}
function checkLoginResult(strResult) {
    if (strResult != "0" && strResult != "1") {
        var strUrlLogin = window.location.href;
        if (strUrlLogin.indexOf("#") != -1) {
            strUrlLogin = strUrlLogin.substring(0, strUrlLogin.indexOf("#"));
        }
        window.location.href = strUrlLogin;
    } else if (strResult == "1") {
        showMessageLogin("lock");
    }
    else {
        showMessageLogin("login");
    }
}
function showMessageLogin(str) {
    var strHtml = "";
    if (str == "lock") {
        $("div_ShowPopup").innerHTML = popupMess(langSI()[0][16]);
        $("divPopupSignIn").style.width = "380px";
    }
    else if (str == "login") {        
        $("div_ShowPopup").innerHTML = popupMess(langSI()[0][0]);
        $("divPopupSignIn").style.width = "380px";
    }
    else if (str == "deal") {    
        $("div_ShowPopup").innerHTML = popupMessDeal(langMess()[0][9]);
        $("divPopupSignIn").style.width = "580px";
    }
    $('div_Shadow').style.display = "block";
    $("divPopupSignIn").style.display = "block";
    $("div_ShowPopup").style.display = "block";
    $("div_CloseWI").onclick = closeBox;
    resizeBox();
}
// show popup dang nhap
function showPopUpSIBuy() {
    $("div_ShowPopup").innerHTML = popupSignIn();
    $('div_Shadow').style.display = "block";
    $("div_ShowPopup").style.display = "block";
    $("div_CloseWI").onclick = closeBox;
    $("btnSIB").onclick = checkLoginPopUp;
    $("txtPEMail").onkeypress = PressKey;
    $("txtPPass").onkeypress = PressKey;
    $("btnPLostPass").onclick = showLostPass;
    if (GetCookie("email")) {
        $("divPRemem").down("div").addClassName("checkedSigninPU");
        $("txtPEMail").value = GetCookie("email");
    }
    $('divPRemem').observe('click', function() {
        $("divPRemem").down("div").hasClassName("checkedSigninPU") ? $("divPRemem").down("div").removeClassName("checkedSigninPU") : $("divPRemem").down("div").addClassName("checkedSigninPU");
    });
    $("txtPEMail").focus();
    loadTextboxEvent($("div_ShowPopup"));
    resizeBox();
}
function checkLoginPopUp() {
    if ($("txtPEMail").value.length == 0 || $("txtPPass").value.length == 0) {
        $("pPopupMess").innerHTML = langMess()[0][3];
    }
    else if (checkMail($("txtPEMail").value) == true) {
        if ($("divPRemem").childNodes[0].className.indexOf("checkedSigninPU") != -1) {
            SetCookie("email", $("txtPEMail").value, 60);
        }
        else{
            DeleteCookie("email", "");
        }
        var strUrlLocation = window.location.href;
        var strUrlLink = strPathLink_All;
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            strUrlLink = strPathLink_All + "yahoo/";
        }
        utils.call(strUrlLink + "xl_Login_2)", { "flag": "check", "email": $("txtPEMail").value, "pass": $("txtPPass").value }, "POST", "checkLoginPopUpResult", "checkLoginPopUpResult");
    }
    else if (checkMail($("txtPEMail").value) == false) {
        $("pPopupMess").innerHTML = langMess()[0][4];
    }
}
function checkLoginPopUpResult(strResult) {
    if (strResult != "0") {
        var strUrlLogin = window.location.href;
        if (strUrlLogin.indexOf("#") != -1) {
            strUrlLogin = strUrlLogin.substring(0, strUrlLogin.indexOf("#"));
        }
        if (strUrlLogin.indexOf("MuaDealChuaDangNhap") != -1 || strUrlLogin.indexOf("MuaDealChuaDangNhap") != -1) {
            try {
                var strUrlLocation = window.location.href;
                var strUrlLink = strPathLink_All;
                if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
                    strUrlLink = strPathLink_All + "yahoo/";
                }
                window.location.href = strUrlLink + "test.jsp";
            } catch (ex) { window.location.href = strUrlLogin; }
        }
        else{
            window.location.href = strUrlLogin;
        }
    } else {
        $("pPopupMess").innerHTML = langSI()[0][0];
    }
}
// quen password
function showLostPass() {
    $("div_ShowPopup").innerHTML = popupLostPass();
    $('div_Shadow').style.display = "block";
    $("div_ShowPopup").style.display = "block";
    $("div_CloseWI").onclick = closeBox;
    $("btnSendPass").onclick = sendLostPass;
    $("txtForgotPass").focus();
    loadTextboxEvent($("div_ShowPopup"));
    resizeBox();
}
//gui email quen pass
function sendLostPass() {
    if (checkMail($("txtForgotPass").value) == true) {
        $("btnSendPass").onclick = "";
        var strUrlLocation = window.location.href;
        var strUrlLink = strPathLink_All;
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            strUrlLink = strPathLink_All + "yahoo/";
        }
        utils.call(strUrlLink + "login.aspx", { "flag": "lostpass", "email": $("txtForgotPass").value }, "POST", "sendLostPassResult", "sendLostPassResult");
    } else if ($("txtForgotPass").value.length == 0) {
        $("pMessLostPass").innerHTML = langMess()[0][5];
    }
    else if (checkMail($("txtForgotPass").value) == false) {
        $("pMessLostPass").innerHTML = langMess()[0][4];
    }
}

//ket qua tra ve tu server
function sendLostPassResult(strResult) {
    if (strResult == "3") {
        $("pMessLostPass").className = $("pMessLostPass").className.replace("color20", "color10");
        $("pMessLostPass").innerHTML = langMess()[0][11];
        $("btnSendPass").onclick = sendLostPass;
    } else {
        $("txtForgotPass").value = "";
        $("pMessLostPass").className = $("pMessLostPass").className.replace("color10", "color20");
        $("pMessLostPass").innerHTML = langMess()[0][0];
    }
}
function radioSignUp(e) {
    var eID = utils.getElementFireEvent(e);
    $(eID.up("li")).select('div').invoke('removeClassName', 'checkedDK');
    $(eID.parentNode.id).down('div').addClassName('checkedDK');
    $(eID.parentNode.id).down('input').checked = true;
    //alert(eID.up("li"));
}
function onChangeCities(id, value) {
    var selectedValue = value;
};

// tao user moi
function SignUp() {
    var i = 0;
    if ($('txtSUEmail')) {
        if (checkMail($('txtSUEmail').value) == false) {
            showError('pSUEmail', 'pMessDK pMessFail', langSU()[0][7], 'block');
            i = 1;
        }
        else {
            showError('pSUEmail', 'pMessDK', '', 'block');
        }
    }
    if ($('txtSUNick')) {
        if (utils.trim($('txtSUNick').value) == "" || utils.trim($('txtSUNick').value).length >15) {
            showError('pSUNick', 'pMessDK pMessFail', langSU()[0][2], 'block');
            i = 1;
        }
        else if ($('txtSUNick').value.match(/\s/g)) {
            showError('pSUNick', 'pMessDK pMessFail', langSU()[0][9], 'block');
            i = 1;
        }
        else if (ClickRegExpChar($('txtSUNick').value) == false) {
            showError('pSUNick', 'pMessDK pMessFail', langSU()[0][8], 'block');
            i = 1;
        }
        else {
            showError('pSUNick', 'pMessDK', '', 'block');
        }
    }
    if ($('cbFemale')) {
        if ($('cbFemale').checked == false && $('cbMale').checked == false) {
            showError('pGender', 'pMessDK pMessFail', langSU()[0][10], 'block');
            i = 1;
        }
        else {
            showError('pGender', 'pMessDK', '', 'block');
        }
    }
    if ($('ddlMonth')) {
        if ($('ddlYear').selectedIndex == 0 || $('ddlMonth').selectedIndex == 0 || $('ddlDate').selectedIndex == 0) {
            showError('pNgaySinh', 'pMessDK pMessFail', langSU()[0][4], 'block');
            i = 1;
        }
        else {
            showError('pNgaySinh', 'pMessDK', '', 'block');
        }
    }
    if ($('txtSUPass')) {
        if (utils.trim($('txtSUPass').value).length < 6 || utils.trim($('txtSUPass').value).length > 15) {
            showError('pSUPass', 'pMessDK pMessFail', langSU()[0][2], 'block');
            i = 1;
        }
        else if ($('txtSURePass').value != $('txtSUPass').value) {
            showError('pSURePass', 'pMessDK pMessFail', langSU()[0][3], 'block');
            i = 1;
        }
        else {
            showError('pSUPass', 'pMessDK', '', 'block');
            showError('pSURePass', 'pMessDK', '', 'block');
        }
    }

    if (i == 0) {
        $('btnSignUp').onclick = "";
        //_gaq.push(['_trackPageview', '/DangKyNhanh']);
        $('form1').submit();
        return false;
    }
}

// tao user moi
function SignUpBuy() {
    var i = 0;
    if ($('txtSUBEmail')) {
        if (checkMail($('txtSUBEmail').value) == false) {
            showErrorBuy('pSUBEmail', langSU()[0][7], 'block');
            i = 1;
        }
        else {
            showErrorBuy('pSUBEmail', '', 'block');
        }
    }
    /* if ($('ddlDate_ddl')) {
    if ($('ddlDate').value == "0" || $('ddlMonth').value == "0" || $('ddlYear').value == "0") {
    showErrorBuy('pSUBirthDay', langSU()[0][4], 'block');
    i = 1;
    }
    else {
    showErrorBuy('pSUBirthDay', '', 'block');
    }
    }*/
    /*if ($('cbFemale')) {

        if ($('cbFemale').checked == false && $('cbMale').checked == false) {            
    showErrorBuy('pGender', langSU()[0][10], 'block');
    i = 1;
    }
    else {
    showErrorBuy('pGender', "", 'block');
    }
    }*/
    if ($('txtSUBNick')) {
        if (utils.trim($('txtSUBNick').value) == "" || utils.trim($('txtSUBNick').value).length > 15) {
            showErrorBuy('pSUBNick', langSU()[0][2], 'block');
            i = 1;
        }
        else if ($('txtSUBNick').value.match(/\s/g)) {
            showErrorBuy('pSUBNick', langSU()[0][9], 'block');
            i = 1;
        }
        else if (ClickRegExpChar($('txtSUBNick').value) == false) {
            showErrorBuy('pSUBNick', langSU()[0][8], 'block');
            i = 1;
        }
        else {
            showErrorBuy('pSUBNick', '', 'block');
        }
    }
    if ($('txtSUBPass')) {
        if (utils.trim($('txtSUBPass').value).length < 6 || utils.trim($('txtSUBPass').value).length > 15) {
            showErrorBuy('pSUBPass', langSU()[0][2], 'block');
            i = 1;
        }
        else if ($('txtSUBRePass').value != $('txtSUBPass').value) {
            showErrorBuy('pSUBRePass', langSU()[0][3], 'block');
            i = 1;
        }
        else {
            showErrorBuy('pSUBPass', '', 'block');
            showErrorBuy('pSUBRePass', '', 'block');
        }
    }

    if (i == 0) {
        $('btnSUBuy').onclick = "";
        //$('btnSUBuy').up('form').submit();
        $("form1").submit();
        return false;
    }
}

function resetPass() {
    if ($('txtResetPass')) {
        if (utils.trim($('txtResetPass').value).length < 6 || utils.trim($('txtResetPass').value).length > 15) {
            $('messRePass').innerHTML = langSU()[0][2];
            $('messRePass').style.display = 'block';
            i = 1;
        }
        else if ($('txtResetRPass').value != $('txtResetPass').value) {
            $('messRePass').innerHTML = langSU()[0][3];
            $('messRePass').style.display = 'block';
            i = 1;
        }
        else {
            $('messRePass').innerHTML = "";
            $('messRePass').style.display = 'block';
            var strUrlLocation = window.location.href;
            var strUrlLink = strPathLink_All;
            if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
                strUrlLink = strPathLink_All + "yahoo/";
            }
            utils.call(strUrlLink + "login.aspx", { "flag": "resetpass", "pass": $("txtResetPass").value, "code": strIDCode }, "POST", "resetPassResult", "resetPassResult");
        }
    }
}
function resetPassResult(strResult) {
    if (strResult == "1") {
        window.location.href = window.location.href;
    } else if (strResult == "0") {
        $('messRePass').innerHTML = langSI()[0][16];
        $('messRePass').style.display = 'block';
    }
    else {
        $('messRePass').innerHTML = langMess()[0][6];
        $('messRePass').style.display = 'block';
    }
}
//hien thi loi khi nhap sai
function showError(id, strClass, strMess, display) {
    $(id).update("<i class='iMessDK'></i><span class='spMessDK'>" + strMess + "</span>");
    $(id).style.display = display;
    $(id).className = strClass;
}
//hien thi loi khi nhap sai
function showErrorBuy(id, strMess, display) {
    $(id).update(strMess);
    $(id).style.display = display;
    $(id).style.marginLeft = "122px";
}

 